package com.li.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.li.constant.MessageConstant;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.entity.Result;
import com.li.pojo.Menu;
import com.li.service.MenuService;
import com.li.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    @Autowired
    private JedisPool jedisPool;

    // 增加
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu)  {
        jedisPool.getResource().set("fail", "");
        menuService.add(menu);
        return new Result(true, "");
    }

    // 删除
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id)  {
        try {
            jedisPool.getResource().set("fail", "");
            menuService.deleteById(id);
            return new Result(true, "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "删除菜单失败！存在关联");
        }
    }

    // 修改
    @RequestMapping("/updateById")
    public Result updateById(@RequestBody Menu menu) {
        jedisPool.getResource().set("fail", "");
        menuService.updateById(menu);
        return new Result(true, "");
    }

    // 修改时回显数据
    @RequestMapping("/findById")
    public Result findById(Integer id)  {
        jedisPool.getResource().set("fail", "");
        Menu menu = menuService.findById(id);
        return new Result(true, "", menu);
    }


    // 查询(带条件)
    @RequestMapping("/findAll")
    public Result findAll(@RequestBody QueryPageBean queryPageBean)  {
        Jedis resource = jedisPool.getResource();
        resource.set("fail", "");
        resource.close();
        PageResult pageResult = menuService.findAll(queryPageBean);
        return new Result(true, "", pageResult);
    }

    // 动态显示菜单
    @RequestMapping("/findMenus")
    public Result findMenus()  {

            User user1 = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            com.li.pojo.User user = userService.findByUsername(user1.getUsername());
            Integer userId = user.getId();
            List<Menu> menus = menuService.findMenusByUserId(userId);
            return new Result(true, "", menus);

    }

    // 弹出新增窗口是查询所有父菜单
    @RequestMapping("/findParentMenuIds")
    public Result findParentMenuIds()  {
        List<Map<String, Object>> parentMenuIds = menuService.findParentMenuIds();
        return new Result(true, "",parentMenuIds);
    }

    // 查询所有权限
    @RequestMapping("/findAllMenu")
    public Result findAllMenu() throws Exception {

        List<Menu> menus = menuService.findAllMenu();

        return new Result(true,"",menus);
    }

    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer roleId){

       List<Integer> ids= menuService.findMenuIdsByRoleId(roleId);

       return new Result(true,"",ids);
    }
}
